﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Interfaces;
using Core.Interfaces.IDAO;
using Core.Interfaces.IPresenters;
using Core.Unity;
using Core.Views;
using Microsoft.Practices.Unity;    
namespace Core.Presenters
{
    public class CustomersPresenter
    {
        private ICustomerDAO _customerDAO;
        private IJuridicalPersonDAO _juridicalPersonDAO;
        private IPrivatePersonDAO _privatePersonDAO;
        private List<ICustomer> _curstomers;
        private  IAbstractDataView _view;
 
        public CustomersPresenter(IAbstractDataView view)
        {
            _customerDAO = ResolverContainer.Resolver.Resolve<ICustomerDAO>();
            _juridicalPersonDAO = ResolverContainer.Resolver.Resolve<IJuridicalPersonDAO>();
            _privatePersonDAO = ResolverContainer.Resolver.Resolve<IPrivatePersonDAO>();
            _curstomers = _customerDAO.GetAll(false);
            view.AddRecord += AddRecord;
            view.DeleteRecord += DeletRecord;
            view.UpdateRecord += Update;
            view.Refreh += Refreh;
            view.Data.DataSource = _curstomers;
            view.Data.ResetBindings(true);
            view.ViewName = "Просмотр покупателей";
            _view = view;
            view.Show();
        }

        private void AddRecord(object sender,EventArgs eventArgs)
        {
            //var custP = new CustomerPresenter(new CustomerView());
            var choice = new CustomerChoice();
            choice.ShowDialog();
            
        }

      
        private void DeletRecord(object sender, EventArgs eventArgs)
        {
            if (_view.Data.Current != null)
            {
                var current = _view.Data.Current;
                _customerDAO.Delete((IDomainObject)current);
                Refreh(this, null);
            }
        }

        private void Refreh(object sender, EventArgs eventArgs)
        {
            _curstomers = _customerDAO.GetAll(false);
            _view.Data.DataSource = _curstomers;
            _view.Data.ResetBindings(true);
        }


        private void Update(object sender, EventArgs eventArgs)
        {
            if (_view.Data.Current != null)
            {
                var current = (ICustomer)_view.Data.Current;
                object additional = null;
                if (current.IsPrivate())
                {
                    additional = _privatePersonDAO.GetAll(false).First(x => x.CustomerID == current.CustomerID);
                    var ppPrsenter = new PrivatePersonPresenter(new PrivatePirsonView(),(IPrivatePerson) additional);
                     
                }
                else
                {
                     additional = _juridicalPersonDAO.GetAll(false).First(x => x.CustomerID == current.CustomerID);
                    new JuridicalPersonPresenter(new JurahialPersonView(), (IJuridicalPerson) additional);
                }
                //var upd = new CustomerPresenter(new CustomerView(), current, additional);
            }
        }
    }
}
